﻿// ***********************************************************************
// Assembly         : DonNicky.Common
// Author           : donnicky
// Created          : 11-19-2012
//
// Last Modified By : donnicky
// Last Modified On : 11-16-2012
// ***********************************************************************
using System;
using System.ComponentModel;

namespace DonNicky.Common
{
    /// <summary>
    /// Defines localized display name (and its plural form) for a type.
    /// </summary>
    [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event)]
    public class LocalizedDisplayNameAttribute : DisplayNameAttribute
    {
        private readonly Type _resourceManagerProvider;

        private readonly string _pluralResourceName;
        /// <summary>
        /// Initializes a new instance of the <see cref="LocalizedDisplayNameAttribute" /> class.
        /// </summary>
        /// <param name="resourceManagerProvider">The resource manager provider.</param>
        /// <param name="resourceName">Name of the resource.</param>
        /// <param name="pluralResourceName">Name of the plural resource.</param>
        public LocalizedDisplayNameAttribute(Type resourceManagerProvider, string resourceName, string pluralResourceName = "")
            : base(resourceName)
        { 
            _resourceManagerProvider = resourceManagerProvider;
            _pluralResourceName = pluralResourceName;
        }

        /// <summary>
        /// Gets the display name for a property, event, or public void method.
        /// </summary>
        /// <value>The display name.</value>
        /// <returns>The display name.</returns>
        public override string DisplayName
        {
            get
            {
                return _resourceManagerProvider.GetResourceValue(base.DisplayName);
            }
        }

        /// <summary>
        /// Gets the plural display name for a property, event, or public void method.
        /// </summary>
        /// <value>The display name in plural form.</value>
        public string PluralDisplayName
        {
            get
            {
                return _resourceManagerProvider.GetResourceValue(_pluralResourceName);
            }
        }
    }
}
